#include "KSADAnalyzeEvent.hh"
#include "KSADGlobals.hh"
#include "TH3D.h"

KSADAnalyzeEvent::KSADAnalyzeEvent()
{
	Init();
}

void KSADAnalyzeEvent::Init()
{
	h3 = 			0;
	msgString = 		"";
	isPossible =		true;
	leftXPos =		0;
	bottomYPos =		0;
	startTime =		0;
	azimuthalAngle =	0;
	azimuthalLow =		0;
	azimuthalHigh =		0;
	azimuthalShift =	0;
	azimuthalShiftLow =	0;
	azimuthalShiftHigh =	0;
	polarAngle =		0;
	polarLow =		90*deg;
	polarHigh =		180*deg;
	polarShift =		0;
	polarShiftLow =		0;
	polarShiftHigh =	0;
	energy =		0;
	xPos =			0;
	yPos =			0;
	cosAzimuthal =		0;
	sinAzimuthal =		0;
}

KSADAnalyzeEvent::~KSADAnalyzeEvent()
{
	if(dataFilePath == "") if(h3) h3->Delete();
}

void KSADAnalyzeEvent::SetH3(TH3D* newH3)
{
	h3 = newH3;
}

TH3D* KSADAnalyzeEvent::GetH3()
{
	if(h3) return h3;

	G4cout << "KSADANALYZEEVENT: TRYING TO ACCESS H3 WITHOUT SETTING IT FIRST\n";

	return 0;
}

void KSADAnalyzeEvent::SetPossible(bool input)
{
	isPossible = input;
}

bool KSADAnalyzeEvent::IsPossible()
{
	return isPossible;
}

void KSADAnalyzeEvent::AddToMsg(G4String newString)
{
	std::stringstream stringStream;
	stringStream << msgString << newString;
	msgString = stringStream.str();
}

G4String KSADAnalyzeEvent::GetMsg()
{
	return msgString;
}

void KSADAnalyzeEvent::SetLeftXPos(double newLeftXPos){leftXPos = newLeftXPos; UpdateXPos();}

void KSADAnalyzeEvent::SetBottomYPos(double newBottomYPos){bottomYPos = newBottomYPos; UpdateYPos();}

void KSADAnalyzeEvent::SetStartTime(double newStartTime){startTime = newStartTime;}

void KSADAnalyzeEvent::SetAzimuthalAngle(double newAzimuthalAngle)
{
	azimuthalAngle = newAzimuthalAngle; UpdateXPos(); UpdateYPos();
	cosAzimuthal = cos(azimuthalAngle/radian);
	sinAzimuthal = sin(azimuthalAngle/radian);
}

void KSADAnalyzeEvent::SetAzimuthalLow(double newAzimuthalLow){azimuthalLow = newAzimuthalLow;}

void KSADAnalyzeEvent::SetAzimuthalHigh(double newAzimuthalHigh){azimuthalHigh = newAzimuthalHigh;}

void KSADAnalyzeEvent::SetAzimuthalShift(double newAzimuthalShift){azimuthalShift = newAzimuthalShift; UpdateXPos(); UpdateYPos();}

void KSADAnalyzeEvent::SetAzimuthalShiftLow(double newAzimuthalShiftLow){azimuthalShiftLow = newAzimuthalShiftLow;}

void KSADAnalyzeEvent::SetAzimuthalShiftHigh(double newAzimuthalShiftHigh){azimuthalShiftHigh = newAzimuthalShiftHigh;}

void KSADAnalyzeEvent::SetPolarAngle(double newPolarAngle){polarAngle = newPolarAngle;}

void KSADAnalyzeEvent::SetPolarLow(double newPolarLow){if(newPolarLow > 0) polarLow = newPolarLow;}

void KSADAnalyzeEvent::SetPolarHigh(double newPolarHigh){polarHigh = newPolarHigh;}

void KSADAnalyzeEvent::SetPolarShift(double newPolarShift){polarShift = newPolarShift; UpdateXPos(); UpdateYPos();}

void KSADAnalyzeEvent::SetPolarShiftLow(double newPolarShiftLow){polarShiftLow = newPolarShiftLow;}

void KSADAnalyzeEvent::SetPolarShiftHigh(double newPolarShiftHigh){polarShiftHigh = newPolarShiftHigh;}

void KSADAnalyzeEvent::AddEnergy(double newEnergy){energy = energy + newEnergy;}

void KSADAnalyzeEvent::UpdateXPos()
{
	xPos = cos(azimuthalAngle/radian + 90*deg/radian)*azimuthalShift + cos(azimuthalAngle/radian)*polarShift + leftXPos;
}

void KSADAnalyzeEvent::UpdateYPos()
{
	yPos = sin(azimuthalAngle/radian + 90*deg/radian)*azimuthalShift + sin(azimuthalAngle/radian)*polarShift + bottomYPos; 
}

double KSADAnalyzeEvent::GetXPos()
{
	return xPos;
}

double KSADAnalyzeEvent::GetYPos()
{
	return yPos;
}

double KSADAnalyzeEvent::GetPrePolarX()
{
	double xPos = cos(azimuthalAngle/radian + 90*deg/radian)*azimuthalShift + leftXPos;

	return xPos;
}

double KSADAnalyzeEvent::GetPrePolarY()
{
	double yPos = sin(azimuthalAngle/radian + 90*deg/radian)*azimuthalShift + bottomYPos; 

	return yPos;
}

double KSADAnalyzeEvent::GetLeftX(){return leftXPos;}

double KSADAnalyzeEvent::GetBottomY(){return bottomYPos;}

double KSADAnalyzeEvent::GetStartTime(){return startTime;}

double KSADAnalyzeEvent::GetAzimuthalAngle(){return azimuthalAngle;}

double KSADAnalyzeEvent::GetAzimuthalLow(){return azimuthalLow;}

double KSADAnalyzeEvent::GetAzimuthalHigh(){return azimuthalHigh;}

double KSADAnalyzeEvent::GetAzimuthalShift(){return azimuthalShift;}

double KSADAnalyzeEvent::GetAzimuthalShiftLow(){return azimuthalShiftLow;}

double KSADAnalyzeEvent::GetAzimuthalShiftHigh(){return azimuthalShiftHigh;}

double KSADAnalyzeEvent::GetPolarAngle(){return polarAngle;}

double KSADAnalyzeEvent::GetPolarLow(){return polarLow;}

double KSADAnalyzeEvent::GetPolarHigh(){return polarHigh;}

double KSADAnalyzeEvent::GetPolarShift(){return polarShift;}

double KSADAnalyzeEvent::GetPolarShiftLow(){return polarShiftLow;}

double KSADAnalyzeEvent::GetPolarShiftHigh(){return polarShiftHigh;}

double KSADAnalyzeEvent::GetEnergy(){return energy;}

double KSADAnalyzeEvent::GetCosAzimuthal(){return cosAzimuthal;}

double KSADAnalyzeEvent::GetSinAzimuthal(){return sinAzimuthal;}
